using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Tilemaps;

public class AstarDebug : MonoBehaviour
{
    private static AstarDebug instance;
    public static AstarDebug MyInstance
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType<AstarDebug>();
            }
            return instance;
        }
    }
    [SerializeField]
    private Canvas canvas;
    [SerializeField]
    private Grid grid;
    [SerializeField]
    private Color openColor, closedColor, pathColor, startColor, targetColor;
    [SerializeField]
    private GameManager textPrefab;
    [SerializeField]
    private Tile tile;
    private List<GameObject> debugObjects= new List<GameObject>();
    [SerializeField]
    Tilemap tilemap;
    [SerializeField]
    private GameObject arrowPrefab;
   
    public void CreateTiles
        (
        Vector3Int start, 
        Vector3Int target,
        Dictionary<Vector3Int, Node> allNodes, 
        HashSet<Node> openList, 
        HashSet<Node> closedList, 
        Stack<Vector3Int> path = null
        )
    {
        foreach (Node node in openList)
        {
            ColorTiles(node.Position, openColor);
        }
        foreach (Node node in closedList)
        {
            ColorTiles(node.Position, closedColor);
        }
        if (path != null)
        {
            foreach(Vector3Int pos in path)
            {
                if (pos!=start && pos != target)
                {
                    ColorTiles(pos,pathColor);
                }
            }
        }
        ColorTiles(start, startColor);
        ColorTiles(target, targetColor);
        foreach(KeyValuePair<Vector3Int, Node> node in allNodes)
        {
            if (node.Value.Parent != null && node.Key != target)
            {
                GameObject go = Instantiate(arrowPrefab, canvas.transform);
                go.transform.position =grid.CellToWorld(node.Key); 
                debugObjects.Add(go);
                GenerateDebugText(node.Value, go.GetComponent<AstarDebugText>());
            }
        }
    }
    private void GenerateDebugText(Node node, AstarDebugText debugText)
    {
        if (node.Parent.Position.x< node.Position.x && node.Parent.Position.y==node.Position.y)
        {
            debugText.MyArrow.localRotation = Quaternion.Euler(0, 0, 180);
        }
        else if (node.Parent.Position.x < node.Position.x && node.Parent.Position.y > node.Position.y)
        {
            debugText.MyArrow.localRotation = Quaternion.Euler(0, 0, 135);
        }
        else if (node.Parent.Position.x < node.Position.x && node.Parent.Position.y < node.Position.y)
        {
            debugText.MyArrow.localRotation = Quaternion.Euler(0, 0, 225);
        }
        else if (node.Parent.Position.x == node.Position.x && node.Parent.Position.y > node.Position.y)
        {
            debugText.MyArrow.localRotation = Quaternion.Euler(0, 0, 90);
        }
        else if (node.Parent.Position.x == node.Position.x && node.Parent.Position.y < node.Position.y)
        {
            debugText.MyArrow.localRotation = Quaternion.Euler(0, 0, 270);
        }
        else if (node.Parent.Position.x > node.Position.x && node.Parent.Position.y > node.Position.y)
        {
            debugText.MyArrow.localRotation = Quaternion.Euler(0, 0, 45);
        }
        else if (node.Parent.Position.x > node.Position.x && node.Parent.Position.y < node.Position.y)
        {
            debugText.MyArrow.localRotation = Quaternion.Euler(0, 0, -45);
        }
        debugText.F.text = $"F:{node.F}";
        debugText.G.text = $"G:{node.G}";
        debugText.H.text = $"H:{node.H}";
        debugText.P.text = $"P:{node.Position.x},{node.Position.y}";
    }
    public void ColorTiles(Vector3Int position, Color color)
    {
        tilemap.SetTile(position, tile);
        tilemap.SetTileFlags(position, TileFlags.None);
        tilemap.SetColor(position, color);
        
    }
    public void SwitchDebugMode()
    {
        Color nocolor=tilemap.color;
        nocolor.a = nocolor.a != 0 ? 0:1;
        canvas.enabled = !canvas.enabled;
        tilemap.color = nocolor;
    }
}
